package com.wxapp.pay.service.event.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxapp.common.base.util.redis.RedisUtil;
import com.wxapp.common.constant.base.IBaseListen;
import com.wxapp.common.util.check.CheckUtil;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.pay.api.pojo.pojo.entity.PayLogsDO;
import com.wxapp.pay.api.pojo.pojo.vo.payLogs.QPayLogsAddVo;
import com.wxapp.pay.service.event.PayLogsEvent;
import com.wxapp.pay.service.mapper.PayLogsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author lgl
 */
@Component
@RequiredArgsConstructor
public class PayLogsListener extends IBaseListen<PayLogsEvent, QPayLogsAddVo> {
    private final PayLogsMapper payLogsMapper;

    @Override
    protected Object toDealListen(PayLogsEvent event) throws Exception {
        if (CheckUtil.isNullObject(event.getData())){
            return null;
        }
        var query=new LambdaQueryWrapper<PayLogsDO>();
        query.eq(PayLogsDO::getOrderCode,event.getData().getOrderCode());
         var payLogs = payLogsMapper.selectFirst(query);
         if(CheckUtil.isNullObject(payLogs)){
              payLogs = CopyUtil.copyObjectNew(event.getData(), PayLogsDO::new, null);
              payLogsMapper.insert(payLogs);
         }else {
             CopyUtil.copyObject(event.getData(), payLogs, (s,r)->{
                 r.setUpdateTime(LocalDateTime.now());
                 return null;
             });
             payLogsMapper.updateById(payLogs);
         }

        return null;
    }
}
